package com.itheima.controller;

import com.itheima.pojo.vo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.domain.Student;
import com.itheima.pojo.dto.StudentPageQueryDTO;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/students")
@Slf4j
public class StudentController {

    @Autowired
    private StudentService studentService;

    /**
     * 分页查询所有学生信息
     * @return
     */
    @GetMapping
    public Result list(StudentPageQueryDTO spqdto) {
        log.info("分页查询所有学生信息");
        PageResult<Student> pageResult  = studentService.list(spqdto);

        return Result.success(pageResult);
    }

    /**
     * 根据id查询学生信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getStudentInfoById(@PathVariable Integer id) {
        log.info("根据id查询学生信息：{}",id);
        Student student = studentService.getStudentInfoById(id);
        return Result.success(student);
    }

    /**
     * 添加学生信息
     * @param student
     * @return
     */
    @PostMapping
    public Result addStudent(@RequestBody Student student){
        log.info("添加学生信息：{}",student);
        studentService.addStudentInfo(student);
        return Result.success();
    }

    /**
     * 删除学生信息
     * @param ids
     * @return
     */
    @DeleteMapping("/{ids}")
    public Result deleteStudent(@PathVariable List<Integer> ids){
        log.info("删除学生信息：{}",ids);
        studentService.deleteStudentInfo(ids);
        return Result.success();
    }
    /**
     * 修改学生信息
     * @param student
     * @return
     */
    @PutMapping
    public Result updateStudent(@RequestBody Student student){
        log.info("修改学生信息：{}",student);
        studentService.updateStudentInfo(student);
        return Result.success();
    }

    /**
     *  扣分扣分次数
     * @param id
     * @param score
     * @return
     */
    @PutMapping("violation/{id}/{score}")
    public Result updateViolation(@PathVariable("id") Integer id, @PathVariable("score") Short score){
        log.info("修改学生信息：{}", id ,score);
        studentService.updateViolation(id ,score);
        return Result.success();
    }

}
